﻿using JinfuyuanProject.Entity;

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace JinfuyuanProject.Repository
{
    public class SalaryDetailsRepository:ISalaryDetailsRepository
    {
        private readonly JinfuyuanDbContext _db;
        public SalaryDetailsRepository(JinfuyuanDbContext db)
        {
            _db = db;
        }
        #region 基础方法
        public bool Delete(SalaryDetails salaryDetails)
        {
            _db.Entry(salaryDetails).State = System.Data.Entity.EntityState.Deleted;
            return _db.SaveChanges() > 0;
        }

        public IQueryable<SalaryDetails> GetList()
        {
            return _db.SalaryDetails;
        }

        public SalaryDetails GetModel(int Id)
        {
            SalaryDetails salaryDetails = _db.SalaryDetails.Find(Id);
            return salaryDetails;
        }

        public int Insert(SalaryDetails salaryDetails)
        {
            _db.Entry(salaryDetails).State = System.Data.Entity.EntityState.Added;
            _db.SaveChanges();
            return salaryDetails.Id;
        }

        public bool Update(SalaryDetails salaryDetails)
        {
            _db.Entry(salaryDetails).State = System.Data.Entity.EntityState.Modified;
            return _db.SaveChanges() > 0;
        }
        #endregion
        #region 扩展方法
        // 获取员工的提成薪资信息
        public async Task<SalaryDetails> GetSalaryDetailAsync(int contractId, DateTime startDate, DateTime endDate)
        {
            // 假设提成信息存储在 SalaryDetails 表中
            return await _db.SalaryDetails
                .Where(sd => sd.ContractId == contractId && sd.CreateTime >= startDate && sd.CreateTime <endDate)
                .FirstOrDefaultAsync();
        }
        // 保存薪资明细
        public async Task SaveSalaryDetailAsync(SalaryDetails salaryDetail)
        {
            _db.Entry(salaryDetail).State = System.Data.Entity.EntityState.Modified;
            await _db.SaveChangesAsync();
        }
        #endregion
    }
}
